﻿using System;
using System.Collections;
using System.Text;
using System.Web;
using Eff.Utils;

namespace Eff.Extensions
{
    /// <summary>
    /// string 扩展方法
    /// </summary>
    public static class StringExt
    {
        /// <summary>
        /// 判断输入字符串是否为空,简单偷懒封装
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static bool IsEmptyOrNull(this string strInput)
        {
            return string.IsNullOrEmpty(strInput);
        }
        /// <summary>
        /// 判断输入字符串是否为非空,简单偷懒封装
        /// </summary>
        /// <param name="strInput"></param>
        /// <returns></returns>
        public static bool IsNotEmptyOrNull(this string strInput)
        {
            return !string.IsNullOrEmpty(strInput);
        }
        /// <summary>
        /// 合并一组可枚举的对象为字符串,没有null
        /// </summary>
        /// <param name="enumerable">被枚举对象</param>
        /// <param name="seperator">分隔符</param>
        /// <returns></returns>
        public static string ConcatEnumerable(this IEnumerable enumerable, string seperator)
        {
            IEnumerator enumerator = enumerable.GetEnumerator();
            if (!enumerator.MoveNext())
                return string.Empty;
            var text = new StringBuilder(128);
            text.Append(enumerator.Current);
            while (enumerator.MoveNext() && enumerator.Current != null)
                text.Append(seperator + enumerator.Current);
            return text.ToString();
        }

        /// <summary>
        /// 对字符串某些位置进行替换屏蔽
        /// </summary>
        /// <param name="str"></param>
        /// <param name="leftCount">左边显示明文个数</param>
        /// <param name="rightCount">右边显示明文个数</param>
        /// <returns></returns>
        public static string ReplaceSensitive(this string str, int leftCount, int rightCount)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }
            int num1 = str.Length;
            if (leftCount < 0)
            {
                leftCount = 0;
            }
            if (leftCount > num1)
            {
                leftCount = num1 - 1;
            }
            if ((leftCount + rightCount) > num1)
            {
                rightCount = num1 - leftCount;
            }
            char[] chArray1 = str.ToCharArray();
            int num2 = leftCount;
            int num3 = chArray1.Length - rightCount;
            while (num2 < num3)
            {
                chArray1[num2] = '*';
                num2++;
            }
            return new string(chArray1);
        }

        #region 编码，解码
        ///<summary>
        ///</summary>
        ///<param name="url"></param>
        ///<returns></returns>
        public static string UrlEncode(this string url)
        {
            return HttpUtility.UrlEncode(url);
        }

        ///<summary>
        ///</summary>
        ///<param name="url"></param>
        ///<returns></returns>
        public static string UrlDecode(this string url)
        {
            return HttpUtility.UrlDecode(url);
        }

        ///<summary>
        ///</summary>
        ///<param name="url"></param>
        ///<returns></returns>
        public static string HtmlEncode(this string url)
        {
            return HttpUtility.HtmlEncode(url);
        }

        ///<summary>
        ///</summary>
        ///<param name="url"></param>
        ///<returns></returns>
        public static string HtmlDecode(this string url)
        {
            return HttpUtility.HtmlDecode(url);
        }
        #endregion

        /// <summary>
        /// 在url地址拼凑加上一个参数.
        /// 功能:自动判断连接符号使用&还是?
        ///      query支持多个键值对
        ///      query为空时不处理
        ///      url上如果有query的参数则覆盖
        /// </summary>
        /// <param name="url"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static string AddQuery(this string url, string query)
        {
            if (query.IsEmptyOrNull()) return url;

            if (url.IndexOf("?") == -1)
                url += "?";
            else
                url += "&";

            //去掉url重复的参数
            var arrTem = query.Split(Const.SpiltSign.And, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in arrTem)
            {
                var key = item.Split(Const.SpiltSign.Equal)[0];
                url = url.RemoveUrlParam(key);
            }
            url += query;
            //修正去除参数后地址变成page.aspx?&query=value的情况
            url = url.Replace("?&", "?");
            return url;
        }

        /// <summary>
        /// 去除url上一个query的参数键值
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public static string RemoveUrlParam(this string url, string paramName)
        {
            if (paramName.IsEmptyOrNull()) return url;

            //修正参数匹配到值的问题,如page.aspx?frq=waped ，paramName=waped
            var startIndex = url.IndexOf("?" + paramName, StringComparison.OrdinalIgnoreCase);
            if (startIndex == -1)
                startIndex = url.IndexOf("&" + paramName, StringComparison.OrdinalIgnoreCase);
            if (startIndex == -1) return url;

            var endIndex = url.IndexOf("&", startIndex + 1, StringComparison.OrdinalIgnoreCase);
            endIndex = endIndex == -1 ? url.Length : endIndex;
            return url.Remove(startIndex, endIndex - startIndex);
        }


        /// <summary>
        /// 字符串重复输出
        /// </summary>
        /// <returns></returns>
        public static string RepeatOuptut(this string str, int count = 1)
        {
            if (count < 0)
                throw new ArgumentException("count不能少于0");
            var sb = new StringBuilder(str.Length * count);
            for (int i = 0; i < count; i++)
            {
                sb.Append(str);
            }
            return sb.ToString();
        }
    }
}
